package edu.avans.hartigehap.web.frontend.controller;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import edu.avans.hartigehap.domain.*;
import edu.avans.hartigehap.service.*;

import java.util.*;

import javax.servlet.http.HttpServletRequest;


@RequestMapping("")
@Controller
public class RestaurantFrontendController {
		
	final Logger logger = LoggerFactory.getLogger(RestaurantFrontendController.class);
	
	@Autowired
	private RestaurantService restaurantService;
	@Autowired
	private BillService billService;

	// mapping to "/" is not RESTful, but is for bootstrapping!
	@RequestMapping(value = {"", "/restaurants"}, method = RequestMethod.GET)
	public String listRestaurants(
			Model uiModel
			) {

		// warmup stuff
		Collection<Restaurant> restaurants = restaurantService.findAll();
		uiModel.addAttribute("restaurants", restaurants);
		
		return "frontend/hartigehap/listrestaurants";
	}

	@RequestMapping(value = "/restaurants/{restaurantName}", method = RequestMethod.GET)
	public String showRestaurant(
			HttpServletRequest request,
			Model uiModel, 
			@PathVariable("restaurantName") String restaurantName
			) {
		//logger.info("BillId: " + request.getAttribute("billId"));
		// warmup stuff
		Collection<Restaurant> restaurants = restaurantService.findAll();
		uiModel.addAttribute("restaurants", restaurants);
		
		Restaurant restaurant = restaurantService.fetchWarmedUp(restaurantName);
		uiModel.addAttribute("restaurant", restaurant);

		//DiningTable diningTable = diningTableService.fetchWarmedUp(Long.valueOf(internetTable));
		//uiModel.addAttribute("diningTable", null);


		Long billId = (Long) request.getAttribute("billId");
		logger.info("billId = " + billId);
		Bill bill = billService.findBillById(billId);
		logger.info("bill = " + bill);
		uiModel.addAttribute("bill", bill);

		return "frontend/hartigehap/restaurant";
	}
}
